package com.czmh.etc.onlinepubsys.enums.ue;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 联合电子ETC新系统平台-api状态码枚举对象
 */
@ApiModel("联合电子ETC新系统平台-api状态码枚举对象")
public enum ApiCodeEnum {
    SUCCESS("200", "业务处理成功"),
    CONNECTION_REST("521", "服务器连接失败"),
    SOCKET_TIME_OUT("522", "服务器连接超时"),
    TOKEN_FAIL("523", "授权token获取失败，请联系管理员！"),
    FAIL("FAIL", "业务处理超时");

    /**
     * 编码
     */
    @ApiModelProperty("编码")
    private String code;
    /**
     * 描述
     */
    @ApiModelProperty("描述")
    private String message;

    private ApiCodeEnum(String code, String message) {
        this.code = code;
        this.message = message;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static Map<String, Map<String, Object>> toMap() {
        ApiCodeEnum[] ary = ApiCodeEnum.values();
        Map<String, Map<String, Object>> enumMap = new HashMap<String, Map<String, Object>>();
        for (int num = 0; num < ary.length; num++) {
            Map<String, Object> map = new HashMap<String, Object>();
            String key = ary[num].name();
            map.put("message", ary[num].getMessage());
            enumMap.put(key, map);
        }
        return enumMap;
    }

    @SuppressWarnings({"rawtypes", "unchecked"})
    public static List toList() {
        ApiCodeEnum[] ary = ApiCodeEnum.values();
        List list = new ArrayList();
        for (int i = 0; i < ary.length; i++) {
            Map<String, String> map = new HashMap<String, String>();
            map.put("message", ary[i].getMessage());
            map.put("name", ary[i].name());
            list.add(map);
        }
        return list;
    }

    public static ApiCodeEnum getEnum(String name) {
        ApiCodeEnum[] arry = ApiCodeEnum.values();
        for (int i = 0; i < arry.length; i++) {
            if (arry[i].name().equalsIgnoreCase(name)) {
                return arry[i];
            }
        }
        return null;
    }

    /**
     * 取枚举的json字符串
     *
     * @return
     */
    public static String getJsonStr() {
        ApiCodeEnum[] enums = ApiCodeEnum.values();
        StringBuffer jsonStr = new StringBuffer("[");
        for (ApiCodeEnum senum : enums) {
            if (!"[".equals(jsonStr.toString())) {
                jsonStr.append(",");
            }
            jsonStr.append("{id:'").append(senum).append("',message:'").append(senum.getMessage()).append("'}");
        }
        jsonStr.append("]");
        return jsonStr.toString();
    }

}
